package com.yf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.dto.Page;
import com.yf.entity.Attendance;
import com.yf.mapper.AttendanceMapper;
import com.yf.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public void addAttendance(Attendance attendance) {
        attendanceMapper.addAttendance(attendance);
    }

    @Override
    public void deleteAttendance(Integer attendanceId) {
        attendanceMapper.deleteAttendance(attendanceId);
    }

    @Override
    public void updateAttendance(Attendance attendance) {
        attendanceMapper.updateAttendance(attendance);
    }

    @Override
    public Attendance getAttendanceByAttendanceId(Integer attendanceId) {
        return attendanceMapper.getAttendanceByAttendanceId(attendanceId);
    }

    @Override
    public Page getAttendancePage(Integer pageNo, Integer pageSize) {

        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Attendance> attendanceList = attendanceMapper.getAttendanceList();
        PageInfo<Attendance> info = new PageInfo<>(attendanceList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setPageCount(info.getPages());
        page.setCount((int) info.getTotal());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setData(info.getList());

        return page;
    }


}
